
require(['jquery', 'jquerymobile', 'net', 'dialogs','panle'], function($, m, net, dia,panle) {


    //注销用户后复选框清空
    function clear_checkbox() {
        $('#all_check').removeClass('nor').addClass('sel');
        $('div[name="sbox"]', $('#filter')).each(function(index, val) {
            $(val).removeClass('nor').addClass('sel');
        });
        //$('#filter_btn_applay').hide();
    }

    $("#filter").on( "pagecreate", function() {
        $( "body > [data-role='panel']" ).panel();
        $( "body > [data-role='panel'] [data-role='listview']" ).listview();
    });
    //changePassword页面 设置内容高度是Header剩下的高度
    $("#filter").on( "pageshow", function( event ) {
        console.error("filter");
        window.setBodyOverflow($(document.body));
        $('#filter_content').css('height',($(window).height()-44));
        $('#newsFilter_ul').css('min-height', ($(window).height()-45));
        // 读取以前勾的数据
        var selectBefore = [];
        if (!$.isEmptyObject(localStorage['selected_tags'])) {
            selectBefore = localStorage['selected_tags'].toString().split(',');
            $.each($('div[name="sbox"]', $('#filter')), function(boxIndex, boxData) {
                $.each(selectBefore, function(selIndex, selectData) {
                    if (selectData == $(boxData).attr('alt')) {
                        $(boxData).removeClass('nor').addClass('sel');
                    }
                });
            });

            if ($('div[name="sbox"]', $('#filter')).length == (selectBefore.length - 1)) {
                $('div[name="sbox2"]').removeClass('nor').addClass('sel');
            } else {
                $('div[name="sbox2"]').removeClass('sel').addClass('nor');
            }
        } else {
            clear_checkbox();
        }

        $('div[name="sbox"]', $('#filter')).off('click');
        $('div[name="sbox"]', $('#filter')).on('click', function(event) {
            if ($(this).hasClass('nor'))
                $(this).removeClass('nor').addClass('sel');
            else
                $(this).removeClass('sel').addClass('nor');

            // 如果勾选了数量等于总数 则全选择也勾上
            var selectCount = 0;
            $.each($('div[name="sbox"]', $('#filter')), function(boxIndex, boxData) {
                if ($(boxData).hasClass('sel')) {
                    selectCount++;
                }
            });
            if (selectCount === 0) {
                $('#filter_btn_applay').hide();
            } else {
                $('#filter_btn_applay').show();
            }
            if ($('div[name="sbox"]', $('#filter')).length == selectCount) {
                $('div[name="sbox2"]', $('#filter')).removeClass('nor').addClass('sel');
            } else {
                $('div[name="sbox2"]', $('#filter')).removeClass('sel').addClass('nor');
            }
        });

        $('#all_check').off('click');
        $('#all_check').on('click', function(event) {
            if ($('#all_check').hasClass('nor')) {
                $(this).removeClass('nor').addClass('sel');
                $('div[name="sbox"]', $('#filter')).each(function(index, val) {
                    $(val).removeClass('nor').addClass('sel');
                });
                $('#filter_btn_applay').show();
            } else {
                $(this).removeClass('sel').addClass('nor');
                $('div[name="sbox"]', $('#filter')).each(function(index, val) {
                    $(val).removeClass('sel').addClass('nor');
                });
                $('#filter_btn_applay').hide();
            }


        });
        // Nick added for pull to refresh start
        window.shouldPageRefresh.newsroom = true; 
        // Nick added for pull to refresh end
        window.historyView = [];
    });

    function initPageLoading(wrapper) {
        var $wrapper = $("#" + wrapper),
            $pullDownEl = $wrapper.find("#pullDown");
        if(wrapper == "wrapper_video"){
            $pullDownEl = $wrapper.find("#pullDown_video");
        }
        var $pullDownLabel = $pullDownEl.find(".pullDownLabel");
        if ($wrapper.find("ul").html()) {
            $wrapper.find(".scroller").css(window.getVendorStyle("transform"), "translate(0, 0)");
            $pullDownEl.attr("class", "loading");
            $pullDownLabel.text("Loading...")
        }
    }
    // Nick added for pull to refresh end

    //applay 按钮返回新闻
    $('#filter_btn_applay').on('click', function(evt) {
        var sel_tags = '';
        $.each($('div[name="sbox"]', $('#filter')), function(index, val) {

            if ($(this).hasClass('sel')) {
                sel_tags += $(this).attr('alt')+',';
            }
        });
        localStorage['selected_tags'] = sel_tags;
        // Nick added for pull to refresh start
         // Nick added for pull to refresh end
        var pageId = $('#filter_btn_applay').attr("filterResourcePage");
        var pageWrapper = $('#filter_btn_applay').attr("filterResourceWrapper");
        initPageLoading(pageWrapper);
        $.mobile.newChangePage("#"+pageId,{ transition: "slide",reverse: true,changeHash: false});
     });



    $('#filter_btn_back').on('click', function(evt) {
        var pageId = $('#filter_btn_applay').attr("filterResourcePage");
        if(pageId == "newsroom"){
            window.shouldPageRefresh.newsroom = false;
        } 
        $.mobile.newChangePage("#"+pageId,{ transition: "slide",reverse: true,changeHash: false});
    });

    function compatibility() {
        /* Logon */
        $('#title_filter').parent()
            .css('display', 'block')
            .css('postion', 'relative');

        $('#title_filter').css('postion', 'absulute')
            .css('width', '49px')
            .css('height','20px')
            .css('margin', '8px auto auto auto');
    }

    $(document).ready(function () {
        setTimeout(function() {
            // 兼容其他浏览器
            compatibility();
        },1000);
    });
});

